package com.example.myactspringboot;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

//import com.alibaba.druid.pool.DruidDataSource;

//@Configuration
//@EnableTransactionManagement()
public class SpringContextDataSource implements EnvironmentAware {
	private Environment env;

	@Bean(name = "dsMain")
	@Primary
	public DataSource dsMain() {

		HikariConfig config = new HikariConfig();
		config.setPoolName(env.getProperty("datasource.main.name"));
		config.setDriverClassName(env.getProperty("datasource.main.driverClassName"));
		config.setJdbcUrl(env.getProperty("datasource.main.url"));
		config.setUsername(env.getProperty("datasource.main.username"));
		config.setPassword(env.getProperty("datasource.main.password"));
		config.setConnectionTestQuery(env.getProperty("datasource.main.validationQuery"));
		config.setMinimumIdle(Integer.valueOf(env.getProperty("datasource.main.minIdle")));
		config.setMaximumPoolSize(Integer.valueOf(env.getProperty("datasource.main.maxActive")));
		config.setIdleTimeout(Integer.valueOf(env.getProperty("datasource.main.maxWait")));

		return new HikariDataSource(config);
	}

	@Bean
	public JdbcTemplate jdbcTemplate() {
		JdbcTemplate template = new JdbcTemplate();
		template.setDataSource(dsMain());
		return template;
	}


	@Bean(name = "datasource-clickhouse")
	public DataSource datasourceClickhouse() {
		HikariConfig config = new HikariConfig();
		config.setPoolName(env.getProperty("datasource.clickhouse.name"));
		config.setDriverClassName(env.getProperty("datasource.clickhouse.driverClassName"));
		config.setJdbcUrl(env.getProperty("datasource.clickhouse.url"));
		config.setUsername(env.getProperty("datasource.clickhouse.username"));
		config.setPassword(env.getProperty("datasource.clickhouse.password"));
		config.setConnectionTestQuery(env.getProperty("datasource.clickhouse.validationQuery"));
		config.setMinimumIdle(Integer.valueOf(env.getProperty("datasource.clickhouse.minIdle")));
		config.setMaximumPoolSize(Integer.valueOf(env.getProperty("datasource.clickhouse.maxActive")));
		config.setIdleTimeout(Integer.valueOf(env.getProperty("datasource.clickhouse.maxWait")));

		HikariDataSource ds = new HikariDataSource(config);
		return ds;
	}

	/**
	 * 基于clickHouse的JdbcTemplate
	 * @return
	 */
	@Bean
	public JdbcTemplate clickhouseJdbcTemplate() {
		JdbcTemplate template = new JdbcTemplate();
		template.setDataSource(datasourceClickhouse());
		return template;
	}

	@Bean(name = "transactionManager")
	@Primary
	public PlatformTransactionManager transactionManager(DataSource dsMain) {
		return  new DataSourceTransactionManager(dsMain);
	}


	@Override
	public void setEnvironment(Environment environment) {
		// TODO Auto-generated method stub
		this.env = environment;
	}
}
